package com.dckj.auto.common.utils.file;


import com.dckj.auto.entity.AuthMenu;
import com.xiaoleilu.hutool.setting.dialect.Props;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.List;

public class ForFile {

    private static Props props = new Props("config.properties","UTF-8");
    private static String authFilePath = props.getStr("AUTH_FILE_PATH");
    private static String fileName = "\\AuthCode.java";

    /**
     * 创建文件
     * @param autoCode  文件名称
     * @param content   文件内容
     * @param openFile   是否打开文件
     */
    public static void createFile(String autoCode ,String content,boolean openFile){
        String filePath = authFilePath+fileName;//文件名及类型
        if(StringUtils.isEmpty(autoCode)){
            if(openFile){
                openFile(filePath);
            }
            return;
        }
        File authFile = new File(authFilePath);
        if (!authFile.exists() && !authFile.isDirectory()) {
            authFile.mkdirs();
        }
        File file = new File(filePath);
        try {
            //如果文件不存在，则创建新的文件
            if(!file.exists()){
                file.createNewFile();
                //创建文件成功后，写入内容到文件里
                writeFileContent(filePath, autoCode,content,2);

            }else{
                //修改文件信息
                writeFileContent(filePath,autoCode, content,1);
            }
            if(openFile){
                openFile(filePath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 创建文件
     * @param list  菜单集合
     * @param openFile   是否打开该文件
     */
    public static void createFile(List<AuthMenu> list , boolean openFile){
        String filePath = authFilePath+fileName;//文件名及类型
        FileUtil.deleteFile(filePath);
        File authFile = new File(authFilePath);
        if (!authFile.exists() && !authFile.isDirectory()) {
            authFile.mkdirs();
        }
        File file = new File(filePath);
        try {
            //创建新的文件
            file.createNewFile();
            //创建文件成功后，写入内容到文件里
            writeFileContent(filePath, list);
            if(openFile){
                openFile(filePath);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 打开指定位置的文件
     * @param fileName
     */
    private static void openFile(String fileName){
        try {
            Runtime run = Runtime.getRuntime();
            String[] arg = new String[3];
            arg[0] = "cmd";
            arg[1] = "/c";
            arg[2] = "Explorer.exe /n , /select,"+fileName;
            run.exec(arg);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 向文件中写入内容
     * @param filepath 文件路径
     * @param autoCode
     * @param content
     * @param type
     * @return
     * @throws IOException
     */
    public static void writeFileContent(String filepath,String autoCode,String content,Integer type) throws IOException{
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos  = null;
        PrintWriter pw = null;
        try {
            File file = new File(filepath);//文件路径(包括文件名称)
            //将文件读入输入流
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();
            if(type.equals(2)){//没有内容
                buffer.append("public class AutoCode {"+"\r\n");
                buffer.append("\t/**"+content+"*/"+"\r\n");
                buffer.append("\tpublic static final String "+autoCode+" = \""+autoCode+"\";"+"\r\n");
                buffer.append("}");
            }
            if(type.equals(1)){
                LineNumberReader lnr = new LineNumberReader(new FileReader(filepath));
                lnr.skip(Long.MAX_VALUE);
                int lineNo = lnr.getLineNumber();
                buffer.append("public class AutoCode {"+"\r\n");
                for (int i = 0; i<lineNo-1;i++){
                   buffer.append(br.readLine()+"\r\n");

                }
                buffer.append("\t/**"+content+"*/"+"\r\n");
                buffer.append("\tpublic static final String "+autoCode+" = \""+autoCode+"\";"+"\r\n");
                buffer.append("}");
            }
            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buffer.toString().toCharArray());
            pw.flush();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            //不要忘记关闭
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
    }

    /**
     * 向文件中写入内容
     * @param filepath 文件路径
     * @param list l
     * @throws IOException
     */
    public static void writeFileContent(String filepath,List<AuthMenu> list) throws IOException{
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos  = null;
        PrintWriter pw = null;
        try {
            File file = new File(filepath);//文件路径(包括文件名称)
            //将文件读入输入流
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();
            buffer.append("\n");
            buffer.append("/**"+"\r\n");
            buffer.append(" *菜单权限代码静态类"+"\r\n");
            buffer.append(" */"+"\r\n");
            buffer.append("public class AutoCode {"+"\r\n\n\n");
            Integer parentId = list.get(0).getParentId();
            for (AuthMenu menu : list){
                if(StringUtils.isNotEmpty(menu.getDescribe())){
                    buffer.append("\t/**"+menu.getDescribe()+"*/"+"\r\n");
                }else{
                    buffer.append("\t/**"+menu.getName()+"*/"+"\r\n");
                }
                buffer.append("\tpublic static final String "+menu.getCode()+" = \""+menu.getCode()+"\";");
                if (StringUtils.isNotEmpty(menu.getName())){
                    buffer.append("//"+menu.getName()+"\r\n");
                }else {
                    buffer.append("\r\n");
                }
                if(!menu.getParentId().equals(parentId)){
                    buffer.append("\n\n");
                }
                parentId = menu.getParentId();
            }
            buffer.append("}");
            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buffer.toString().toCharArray());
            pw.flush();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            //不要忘记关闭
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i<9-1;i++){
            createFile("DFGHHGDDFVC", "快递费给你发的开始",false);
        }

    }




}
